package licode.unisop.platform.infrastructure.assembler;

import cn.hutool.json.JSONUtil;
import licode.unisop.provider.vo.SiProps;
import licode.unisop.types.dp.AusPlatformProp;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class TypeConverter {
    public List<String> colonStrToList(String colonStr) {
        List<String> ret;
        // return Arrays.asList(colonStr.split(","));
        if (null == colonStr || colonStr.length() == 0) {
            ret = new ArrayList<>();
        } else {
            try {
                ret = JSONUtil.parseArray(colonStr).toList(String.class);
            } catch (Exception e) {
                ret = Arrays.asList(colonStr.split(","));
            }
        }
        return ret;
    }

    public String listToColonStr(List<String> list) {
        // return String.join(",", list);
        if (null == list || list.size() == 0) {
            return "";
        }
        return JSONUtil.toJsonStr(list);
    }

    public Map<String, Object> colonStrToMap(String colonStr) {
        Map<String, Object> ret;
        if (null == colonStr || colonStr.length() == 0) {
            return new HashMap<>();
        }
        try {
            ret = JSONUtil.parseObj(colonStr);
        } catch (Exception e) {
            e.printStackTrace();
            ret = new HashMap<>();
        }
        return ret;
    }

    public String mapToColonStr(Map<String, Object> source) {
        if (null == source) {
            return "";
        }
        return JSONUtil.toJsonStr(source);
    }

    public AusPlatformProp fromPlProp(String prop) {
        try {
            return JSONUtil.toBean(prop, AusPlatformProp.class);
        } catch (Exception e) {
            return new AusPlatformProp();
        }
    }

    public String toPlProp(AusPlatformProp prop) {
        try {
            return JSONUtil.toJsonStr(prop);
        } catch (Exception e) {
            return "";
        }
    }

    public String propsToString(SiProps props) {
        if (null == props) {
            return null;
        } else {
            return JSONUtil.toJsonStr(props);
        }
    }

    public SiProps stringToProps(String props) {
        if (null == props) {
            return new SiProps();
        } else {
            return JSONUtil.toBean(props, SiProps.class);
        }
    }
}
